package com.example.day12;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.time.Instant;

public class MainActivity extends AppCompatActivity {
   public static TextView textView;
    private MyReceiver receiver;
    private LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        textView = findViewById(R.id.text1);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setReceiver();
            }
        });
      receiver=new MyReceiver();
        //TODO 1：创建过滤器
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.setPriority(100);
//        //TODO 2：调频：
        intentFilter.addAction("android.bawei.action.guwei");
//        intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
//        //TODO 3：注册： 给这个Activity注册
        localBroadcastManager.registerReceiver(receiver,intentFilter);
//registerReceiver(receiver,intentFilter);

        IntentFilter intentFilter2=new IntentFilter();
        intentFilter2.setPriority(2);
////        //TODO 2：调频：
        intentFilter2.addAction("android.bawei.action.guwei");

        registerReceiver(new MyReceiver(),intentFilter2);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);//解除注册
    }

    public void setReceiver(){
        Intent intent=new Intent();
        intent.setAction("android.bawei.action.guwei");
        Bundle bundle=new Bundle();
        bundle.putString("msg","发送广播");
        intent.putExtras(bundle);
//        sendBroadcast(intent);
        localBroadcastManager.sendBroadcast(intent);
        sendOrderedBroadcast(intent,null);
//        sendOrderedBroadcast(intent, null, new MyReceiver2(), new Handler(), Activity.RESULT_OK,
//                "MainActivity发送了一个有序广播", bundle);
//
//        IntentFilter intentFilter2 = new IntentFilter();
//        intentFilter2.addAction("android.bawei.action.guwei");
//        localBroadcastManager.registerReceiver( new MyReceiver2(),intentFilter2);
//
//        localBroadcastManager.sendBroadcast(intent);
    }
}
